//
//  PayWaysAlertView.swift
//  PayAlertView
//
//  Created by Pro on 2018/2/2.
//  Copyright © 2018年 Pro. All rights reserved.
//

import UIKit

class PayWaysAlertView: UIView {
    
    var ConnectView:UIView!

    var ButtonCallBack:((_ type : Int) -> ())?

    
    @IBOutlet weak var weChatBtn: UIButton!
    @IBOutlet weak var aliPayBtn: UIButton!
    @IBOutlet weak var otherMoneyBtn: UIButton!
    @IBOutlet weak var quitViewBtn: UIButton!
    
    
    @IBAction func payViewActions(_ sender: UIButton) {
        
        if sender == weChatBtn && ButtonCallBack != nil {
            ButtonCallBack!(1)
        }
        if sender == aliPayBtn && ButtonCallBack != nil {
            ButtonCallBack!(2)
        }
        if sender == otherMoneyBtn && ButtonCallBack != nil {
            ButtonCallBack!(3)
        }
        if sender == quitViewBtn && ButtonCallBack != nil {
            ButtonCallBack!(4)
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ConnectView = loadViewFromNib()
        addSubview(ConnectView)
        addConstraints()
        initialSetup()
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        ConnectView = loadViewFromNib()
        addSubview(ConnectView)
        addConstraints()
        initialSetup()
    }
    
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }
    
    func addConstraints() {
        ConnectView.translatesAutoresizingMaskIntoConstraints = false
        var constraint = NSLayoutConstraint(item: ConnectView, attribute: .leading,
                                            relatedBy: .equal, toItem: self, attribute: .leading,
                                            multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .trailing,
                                        relatedBy: .equal, toItem: self, attribute: .trailing,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .top, relatedBy: .equal,
                                        toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: ConnectView, attribute: .bottom,
                                        relatedBy: .equal, toItem: self, attribute: .bottom,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
    
    //初始化默认属性配置
    func initialSetup(){
        self.backgroundColor = UIColor(red: 0/225.0, green: 0/225.0, blue: 0/225.0, alpha: 0.3)
    }
    
}
